"""
author: liuxu
date: 2024.12.3
description:
"""
import json
import os
from configparser import ConfigParser

import yaml

from common.logger_ctrl import mylogger


class MyConfigParser(ConfigParser):
    # 重写configparser中的optionxform函数，解决.ini文件中的键option自动转为大写的问题
    def __init__(self, defaults=None):
        ConfigParser.__init__(self, defaults)

    def optionxform(self, optionstr):
        return optionstr

class ReadFileData(object):
    def __init__(self):
        pass
    # 读取yaml文件
    def load_yaml(self, file_path):
        mylogger.info(f'正在加载 {file_path} 文件.....')
        with open(file_path, 'r',encoding='utf-8') as f:
            data = yaml.load(stream=f,Loader=yaml.FullLoader)
        mylogger.info(f'读取到数据 ===> {data}')
        return data
    # 读取json文件
    def load_json(self, file_path):
        mylogger.info(f'正在加载 {file_path} 文件.....')
        with open(file_path, 'r',encoding='utf-8') as f:
            data = json.load(f)
        # mylogger.info(f'读取到数据 ===> {data}')
        return data
    # 读取ini文件
    def load_ini(self, file_path):
        mylogger.info(f'正在加载 {file_path} 文件.....')
        config = MyConfigParser()
        config.read(file_path,encoding='utf-8')
        data = dict(config._sections)
        # mylogger.info(f'读取到数据 ===> {data}')
        return data

_config_file_path = os.path.join(os.path.dirname(__file__), 'global_env.yml')
GLO_VAR= ReadFileData().load_yaml(_config_file_path)

if __name__ == '__main__':
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    yaml_path = os.path.join(BASE_DIR, 'global_config', 'global_env.yml')

    print(GLO_VAR)

